package com.lxd.milktea.controller;

import com.lxd.milktea.VO.ResultVO;
import com.lxd.milktea.dataobject.Material;
import com.lxd.milktea.dataobject.StockInfo;
import com.lxd.milktea.enums.StockEnum;
import com.lxd.milktea.form.StockInfoForm;
import com.lxd.milktea.service.MaterialService;
import com.lxd.milktea.service.StockService;
import com.lxd.milktea.util.ResultVOUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.List;

/**
 * @ProjectName: milktea
 * @Package: com.lxd.milktea.controller
 * @ClassName: StockController
 * @Author: carrymaniac
 * @Description:
 * @Date: 2020/5/12 10:10 上午
 * @Version:
 */
@RestController
public class StockController {

    private final StockService stockService;
    private final MaterialService materialService;

    public StockController(StockService stockService, MaterialService materialService) {
        this.stockService = stockService;
        this.materialService = materialService;
    }

    //
    /**
     * 原料库存更新（进货）参数：List格式，包括 原料ID 原料进货数字
     * todo test
     */
    @PostMapping("/addStock")
    @ApiOperation(value = "原料库存更新（进货）参数：List格式，包括 原料ID 原料进货数字")
    public ResultVO addStock(@RequestBody List<StockInfoForm> list){
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        list.stream().forEach(f ->{
            Material material = materialService.findOne(f.getMaterialId());
            int stock = material.getStock();
            StockInfo stockInfo = new StockInfo();
            stockInfo.setNum(f.getNum());
            stockInfo.setNowStock(stock+f.getNum());
            stockInfo.setType(StockEnum.IN.getType().byteValue());
            stockInfo.setMaterialId(f.getMaterialId());
            stockInfo.setSettleDate(timestamp);
            stockService.addStock(stockInfo);
        });
        return ResultVOUtil.success();
    }


    // todo 给出进货预测
    /**
     * service方法：读取前30天的数据库数据原料stock
     * 返回数据：ID - 原料名 - 数字
     */
    @ApiOperation(value = "给出进货预测,尚未完成")
    public ResultVO prediction(){
        return null;
    }


}
